package gui.crud;

import gui.util.PeriodRenderer;
import gui.util.SimpleDisciplineTableModel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

import domain.Discipline;
import domain.PTime;

public class DisciplineRegisterPanel extends AbstractRegisterPanel {
	
	private JComboBox<Integer> periodField;
	private JComboBox<Integer> hourField;
	private JLabel periodLabel;
	private JLabel durationLabel;
	private JLabel hoursLabel;
	private JLabel prerequisites;

	private JTable tableDisciplines;
	private JScrollPane scrollPane;
	
	private PeriodRenderer renderer;
	
	
	public DisciplineRegisterPanel() {
		super("Registrar Disciplina", 530, 400);
		
	}

	@Override
	protected void registerAct() {
		try {
			String name = getNameField().getText();
			int period = (Integer) periodField.getSelectedItem();
			int hours =  (Integer) hourField.getSelectedItem();
			PTime time = new PTime(hours, 0);
			
			Discipline d = new Discipline(name, period, time);
			//SALVAR NO BANCO DE DADOSSSSSS
			//Por enquanto, para testes, ver isso:
			List<Discipline> l = new ArrayList<Discipline>();
			l.add(d);
			tableDisciplines.setModel(new SimpleDisciplineTableModel(formatData(l)));
			tableDisciplines.updateUI();
			JOptionPane.showMessageDialog(this, "Disciplina cadastrada com sucesso!", "Cadastro", JOptionPane.INFORMATION_MESSAGE);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(this, e.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
		} finally {
			getNameField().setText("");
		}
		

	}

	@Override
	protected void setUpNewComponents() {
		periodField = new JComboBox<Integer>();
		hourField = new JComboBox<Integer>();
		periodLabel = new JLabel("<html><i><b>Periodo:</b></i></html>");
		durationLabel = new JLabel("<html><i><b>Carga Horaria:</b></i></html>");
		hoursLabel = new JLabel("<html><i><b>Hora(s)</b></i></html>");
		
		prerequisites = new JLabel("<html><i><b>Pre-requisitos:</b></i></html>");
		//Getdisciplinas e:
		//List<Discipline> disciplinasPassadas = DisciplineHandler.getAll(); OLHA AQUI!!!!!!!!!!!!<<
		List<Discipline> disciplinasPassadas = new ArrayList<Discipline>();
		Object[][] data = formatData(disciplinasPassadas);
		tableDisciplines = new JTable(new SimpleDisciplineTableModel(data));
		scrollPane = new JScrollPane(tableDisciplines);
		renderer = new PeriodRenderer();
		
		
		try {
			tableDisciplines.setDefaultRenderer(Class.forName("java.lang.Integer"), renderer);
		} catch (ClassNotFoundException e) {System.out.println("erro!");}
		tableDisciplines.setFillsViewportHeight(true);
		
		TableColumn columnPeriod = tableDisciplines.getColumnModel().getColumn(1);
		columnPeriod.setPreferredWidth(22);
		
		//Deve existir globalmente algo que indique a quantidade de periodos no curso!! Por enquanto, 8
		for (int i = 1; i <= 8; i++) {
			periodField.addItem(i);
		}
		periodField.setEditable(false);
		periodField.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				int selectedPeriod = (Integer) periodField.getSelectedItem();
				renderer.setPeriodLimit(selectedPeriod);
				tableDisciplines.updateUI();
			}
		});
		for (int i = 1; i < 7; i++) {
			hourField.addItem(i);
		}
		
		periodField.setBounds(200, 60, 60, 20);
		hourField.setBounds(60, 130, 60, 20);
		periodLabel.setBounds(200, 38, 120, 20);
		durationLabel.setBounds(60, 108 , 120, 20);
		hoursLabel.setBounds(130, 130, 120, 20);
		prerequisites.setBounds(320, 38, 120, 20);
		scrollPane .setBounds(320, 60, 170, 200);
		
	}

	@Override
	protected void addNewComponents() {

		this.add(periodField);
		this.add(hourField);
		this.add(periodLabel);
		this.add(durationLabel);
		this.add(hoursLabel);
		this.add(prerequisites);
		this.add(scrollPane);
	}
	
	private Object[][] formatData(List<Discipline> dataList) {
		Object[][] data = new Object[dataList.size()][2];
		for (int i = 0; i < dataList.size(); i++) {
			data[i][0] = dataList.get(0).getName();
			data[i][1] = dataList.get(0).getPeriod();
		}
		return data;
	}

}
